<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="jhu.profiteer.data.*"%>
<%@ page import="jhu.profiteer.data.Account"%>
<%@ page import="jhu.profiteer.data.Airplane"%>
<%@ page import="jhu.profiteer.data.Character"%>
<%@ page import="jhu.profiteer.data.CharacterJpaController"%>
<%@ page import="jhu.profiteer.data.AccountJpaController"%>
<%@ page import="java.util.Vector"%>
<%@ page import="java.util.List"%>

<html>
  <%
    String pageTitle = "Airplane Information";
    String cssFile = "/profiteer-game/css/profiteer.css";
  %>
  <%@ include file="/WEB-INF/jspf/admin/header.jspf" %>
  <body>
    <%@ include file="/WEB-INF/jspf/title.jspf" %>
    <div class="contentArea Body">
      <div class="pagetitle">Airplane Information</div>
      <div class="pagebody">
        <table>
          <tr>
            <td id="menu"><%@ include file="/WEB-INF/jspf/menu.jspf" %></td>
            <td>
              <div class="divround">
                <%@ include file="/WEB-INF/jspf/admin/notice.jspf" %>
                <%
                  session = request.getSession();
                  //check for a session attribute named “character” of type Character.
                  Character character = (Character)session.getAttribute("character");
                  //check for a session attribute named “airplane” of type Airplane
                  Airplane airplane = (Airplane) session.getAttribute("airplane");
                  //check for a session attribute named “account” of type Account
                  Account account = (Account) session.getAttribute("account");

                  //If no Airplane object is found in the session
                  if (null == airplane)
                  {
                    //If no Character object is found in the session
                    if (null == character)
                    {
                      /* If no Account object is found in the session
                       *  redirect the user to the Index page
                       */
                      if (null == account) this.getServletContext()
                              .getRequestDispatcher("/index")
                              .forward(request, response);
                      else
                      {
                        // search the database for a Character with an accountId equal to the id field of the Account object
                        CharacterJpaController cjc = new CharacterJpaController();
                        if(cjc.testForCharacterWithAccountId(account.getId()))
                        {
                          character = cjc.findCharacter(account.getId());
                          session.setAttribute("character", character);
                        }
                        //If character is NOT found
                        else this.getServletContext()
                                .getRequestDispatcher("/index")
                                .forward(request, response);
                      }
                    }

                    //search the database for an Airplane with a characterId equal to the id in the Character object.
                    AirplaneJpaController ajc = new AirplaneJpaController();

                    if(ajc.testForAirplaneWithCharacterId(character.getId()))
                      airplane = ajc.findAirplaneByCharacterId(character.getId());
                    else
                    {
                      /* If no Account object is found in the session
                       *  redirect the user to the Index page
                       */
                      if (null == account) this.getServletContext()
                              .getRequestDispatcher("/index")
                              .forward(request, response);
                      else
                      {
                        /* Set the damaged field in the Account object to true,
                         * create a database transaction,
                         * save the Account to the database,
                         * commit the transaction,
                         * and redirect the user to the LogOut page with an error
                         */
                        account.setDamaged(true);
                        AccountJpaController accjc = new AccountJpaController();

                        try
                        {
                          accjc.edit(account);
                        }
                        catch(Exception e)
                        {
                          this.getServletContext()
                              .getRequestDispatcher("/index")
                              .forward(request, response);
                        }

                        if (null == notices) notices = new Vector<String>();
                        notices.add("This account has been damaged and needs to be reviewed by a game administrator. An administrator will contact you once the account has been examined and a solution has been implemented.");
                        session.setAttribute("notices", notices);
                        this.getServletContext()
                                .getRequestDispatcher("/logout")
                                .forward(request, response);
                      }
                    }
                  }

                  session.setAttribute("airplane", airplane);
                  //If an Airplane is found (either in the database or the session)
                  /* display the Menu JSP fragment,
                   * a label that says “Airplane Name” followed by the value of the name field of the Airplane object,
                   * a label that says “Current Fuel” followed by the value of the currentFuel field in the Airplane object,
                   * a label that says “Maximum Fuel” followed by the value of the maxFuel field in the Airplane object,
                   * and a label that says “Maximum Cargo Capacity” followed by the value of the maxCargoCapacity field in the Airplane object.
                   */
                  //************ code omitted *********************
                %>

                <fieldset>
                    <legend>Airplane Information</legend>

                    <table>
                        <tr>
                            <td> Airplane Name:</td><td><%=airplane.getName()%></td>
                        </tr>
                        <tr>
                            <td> Current Fuel:</td><td> <%=airplane.getCurrentFuel()%></td>
                        </tr>
                        <tr>
                            <td> Maximum Fuel: </td><td><%=airplane.getMaxFuel()%></td>
                        </tr>
                        <tr>
                            <td> Maximum Cargo Capacity: </td><td><%=airplane.getMaxCargoCapacity()%></td>
                        </tr>
                        <tr/>
                    </table>

                </fieldset>
                <br/>
                <br/>
                <fieldset>
                  <legend>Airplane Cargo</legend>
                  <%
                    //search the database for all the AirplaneCargo items with an airplaneId equal to the id in the Airplane object.
                    AirplaneCargoJpaController acjc = new AirplaneCargoJpaController();
                    List<AirplaneCargo> airplaneCargos = (List<AirplaneCargo>)acjc.findAirplaneCargoEntitiesWithAirplaneId(airplane.getId());

                    if(airplaneCargos.isEmpty())
                    {
                  %>
                  <p>No Cargo</p>
                  <%
                    }
                    else
                    {
                  %>
                  <table rules="rows" width='600'>
                    <tr>
                      <th id="left">Commodity Name</th>
                      <th id="left">Commodity Quantity</th>
                    </tr>
                    <%
                      CommodityJpaController comjc = new CommodityJpaController();

                      for(AirplaneCargo ac : airplaneCargos)
                      {
                        //search the database for a Commodity object with an id equal to the commodityId from the AirplaneCargo object.
                        //commodity = (Commodity) comjc.findCommodity(airplaneCargos.get(i).getId().getCommodityId());

                        /* If no Commodity object can be found,
                         * create a database transaction,
                         * delete the AirplaneCargo object,
                         * commit the transaction,
                         * and move on to the next AirplaneCargo object.
                         * (TODO: Log this for game administration notification.)
                         */
                        if(!comjc.testFindCommodity(ac.getId().getCommodityId()))
                        {
                          acjc.destroy(ac.getId().getAirplaneId(),
                                  ac.getId().getCommodityId());
                        }
                        /* If a Commodity object is found in the database,
                         * the page should display a table row with the name field of the Commodity object as a hyperlink to the CommodityInformation page
                         * (with the id field from the Commodity object as a parameter named commodityId at the end of the URL) in the first column
                         * and the quantity field of the Commodity object in the second.
                         */
                        else
                        {
                          Commodity c = (Commodity)comjc.findCommodity(ac.getId().getCommodityId());
                    %>
                    <tr>
                      <td><a href="/profiteer-game/commodityInformation?commodityId=<%= c.getId() %>" name="commodityId"><%= c.getName() %></a></td>
                      <td><%= ac.getQuantity() %></td>
                    </tr>
                    <%
                        }
                      }
                    %>
                    <tr>
                      <td>&nbsp;</td>
                    </tr>
                  </table>
                  <%
                    }
                  %>
                </fieldset>
                <br/>
                <br/>
                <%
                  GameConfigurationParametersJpaController gcjc =
                          new GameConfigurationParametersJpaController();
                  GameConfigurationParameters gcp =
                          (GameConfigurationParameters)
                          gcjc.findGameConfigurationParameters();


                %>
                <form action="trade" method="post">
                    <fieldset>
                        <legend>Purchase Fuel</legend>
                        Fuel costs <%= gcp.getFuelCost()%> amount per unit.<br />
                        Amount of Fuel to Purchase: <input type="text" name="fuel" size="10"/><br />
                        <input type="submit" name="submit" value="Purchase Fuel">
                    </fieldset>
                </form>
              </div>
            </td>
          </tr>
        </table>
      </div>
    </div>
    <%@ include file="/WEB-INF/jspf/footer.jspf" %>
  </body>
</html>
